package com.example.testapp.activity.fragment;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.example.testapp.activity.http.RetryNetwork;
import com.example.testapp.activity.listener.PwdErrorListener;
import com.example.testapp.activity.model.Info;
import com.example.testapp.activity.ui.DataTask;
import com.example.testapp.activity.util.IBackEventStrategy;
import com.example.testapp.activity.util.LogUtil;
import com.example.testapp.activity.util.ThreadPoolManager;

/**
 * Created by l on 2016/7/16.
 */
public class BaseFragment extends Fragment implements RetryNetwork, PwdErrorListener, View.OnTouchListener, IBackEventStrategy{
    private static final String TAG = "SuperFragment";

    private static final String ARG_PARAM1 = "param1";
    private String  mParam1;

    protected Context mContext;

    protected Activity mActivity;

    protected ThreadPoolManager mThreadPoolManager;

    protected ProgressDialog progress;
    protected Info mInfo;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mContext = getActivity();
        mActivity = getActivity();
        mThreadPoolManager = ThreadPoolManager.getInstance();
        progress = getProgressDialog("正在加载,请稍后...");
        progress.setCancelable(true);

        obtainInfo();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        view.setOnTouchListener(this);
        super.onViewCreated(view, savedInstanceState);
    }

    private ProgressDialog getProgressDialog(String msg) {
        ProgressDialog progressDialog = new ProgressDialog(mContext);
        progressDialog.setMessage(msg);
        progressDialog.setCancelable(true);
        return progressDialog;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            obtainInfo();
        }else {

        }
    }

    /**
     * 获取数据bean的逻辑统一放在这里
     */
    private void obtainInfo() {

    }

    protected DataTask dataTask = new DataTask(0){

    };

    /**
     * UI线程执行一个任务
     *
     * @param run
     */
    private void runOnUI(Runnable run) {
        if(mActivity != null){
            mActivity.runOnUiThread(run);
        }
    }

    /**
     * 显示进度条
     */
    protected void showProgressDialog() {
        runOnUI(new Runnable(){
            @Override
            public void run() {
                   if(progress == null){
                       progress = new ProgressDialog(mContext);
                       progress.setMessage("正在加载,请稍后...");
                       progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                   }
                   progress.setCancelable(true);
                   try{
                       progress.show();
                   }catch (Exception e){
                       LogUtil.e(TAG, "progress show exception");
                   }
            }
        });
    }

    /**
     * 隐藏进度条
     */
    protected void dismissProgressDialog() {
          runOnUI(new Runnable() {
              @Override
              public void run() {
                  if(progress != null){
                      progress.dismiss();
                  }
              }
          });
    }

    @Override
    public boolean backOperate() {
        mInfo = null;
        return false;
    }

    /**
     * 防止View的事件穿透
     */
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }

    @Override
    public void pwdError() {

    }

    @Override
    public void retry() {

    }

    @Override
    public void netError() {

    }
}
